/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import UI_Validation.Purchase;
import com.toedter.calendar.JDateChooser;
import constants.CommonConstants;
import controller.PurchaseController;
import controller.RawMaterialController;
import controller.VendorController;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JComboBox;
import logging.CommonsLogging;
import org.apache.log4j.Logger;
import persistence.rb_vendor_persist;
import vo.rb_purchase;
import vo.rb_vendor;

/**
 *
 * @author Shuhail
 */
public class NewPurchase extends javax.swing.JFrame {

    /**
     * Creates new form NewPurchase
     */
    public NewPurchase() {
        initComponents();
        vendor_list_to_insert=VendorController.getInstance().fetchAll();
        vendor_combo_to_insert=VendorController.getInstance().fetchAll();
       // vendor_names=VendorController.getInstance().returnAllVendorNames();
        raw_mat_names=RawMaterialController.getInstance().fetchAllRawMaterialNames();
       rawMaterialNamesMap=RawMaterialController.getInstance().fetchAllRawMaterialIdAndNames();
        vendorNamesMap = VendorController.getInstance().returnAllVendorIdAndNames();
        payment_mode.setListData(Purchase.getInstance().getPaymentMode());
        
        renderCombo(vendor_combo, vendorNamesMap);
        renderCombo(raw_material_combo, rawMaterialNamesMap);
        
       // this.ok.setEnabled(false);
       // this.Next.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        UnitPrice = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        payment_mode = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        remarks = new javax.swing.JTextPane();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        Next = new javax.swing.JButton();
        quantity = new javax.swing.JFormattedTextField();
        unit_price = new javax.swing.JFormattedTextField();
        amount_paid = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        reset = new javax.swing.JButton();
        total_amount = new javax.swing.JLabel();
        err_label = new javax.swing.JLabel();
        bal_to_pay = new javax.swing.JLabel();
        dateFiled = new com.toedter.calendar.JDateChooser();
        vendor_combo = new javax.swing.JComboBox();
        raw_material_combo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Add New Purchase");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Vendor Name:");

        jLabel2.setText("Raw Material:");

        jLabel3.setText("Quantity :");

        UnitPrice.setText("Unit Price :");

        jLabel4.setText("Raw Material Details");

        jLabel5.setText("Payment Mode :");

        jLabel6.setText("Amount Paid    :");

        payment_mode.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(payment_mode);

        jLabel7.setText("Remarks       :");

        jScrollPane4.setViewportView(remarks);

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        Next.setText("Add Next");
        Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }
        });

        quantity.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        quantity.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                quantityPropertyChange(evt);
            }
        });

        unit_price.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat(""))));
        unit_price.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                unit_pricePropertyChange(evt);
            }
        });

        amount_paid.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat(""))));
        amount_paid.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                amount_paidPropertyChange(evt);
            }
        });

        jLabel8.setText("Date                :");

        reset.setText("Reset");
        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });

        total_amount.setForeground(new java.awt.Color(0, 0, 255));

        err_label.setForeground(new java.awt.Color(255, 51, 51));

        bal_to_pay.setForeground(new java.awt.Color(255, 51, 51));

        dateFiled.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dateFiledPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(jSeparator2)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Next)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addComponent(cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(reset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(amount_paid, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(dateFiled, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(total_amount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(UnitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(27, 27, 27)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(quantity, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                                    .addComponent(unit_price))))))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(62, 62, 62))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(err_label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(bal_to_pay, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vendor_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(raw_material_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(vendor_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(raw_material_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(quantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UnitPrice)
                    .addComponent(unit_price, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(total_amount, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(amount_paid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(bal_to_pay, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(dateFiled, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(48, 48, 48)))
                .addGap(18, 18, 18)
                .addComponent(err_label, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(cancel)
                    .addComponent(Next)
                    .addComponent(reset))
                .addGap(26, 26, 26))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        // TODO add your handling code here:
        this.saveAllData();
        dispose();
        if(logger.isDebugEnabled()) {
            logger.debug("Persisting Purchase deatails");
            logger.debug("Size of list :"+purchase_list.size());
        }
        boolean success = PurchaseController.getInstance().insertPurchase(purchase_list);
        if(logger.isDebugEnabled()) {
        if(!success)
            logger.debug("Not All inserted successfully");
        else 
            logger.debug("All data Inserted Successfully");
        }
    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        // TODO add your handling code here:
        this.setEnabled(false);
        dispose();
    }//GEN-LAST:event_cancelActionPerformed
    
    public static void renderCombo(JComboBox comb,Map<String,String> s) {
        
       Iterator it = s.entrySet().iterator();
       while(it.hasNext()) {
           
           Map.Entry entry =(Map.Entry) it.next();
           comb.addItem(entry.getKey());
       }
    }
    
    public void saveAllData() {
        if(logger.isDebugEnabled())
            logger.debug("**Starting the Fetching of Data**");
        rb_purchase pur = new rb_purchase();
       
        try {
        String vname = this.vendor_combo.getSelectedItem().toString();
        String vid=vendorNamesMap.get(vname);
        String rname = this.raw_material_combo.getSelectedItem().toString();
        String cheque = this.payment_mode.getSelectedValue().toString();
        double amount =((Number)this.amount_paid.getValue()).doubleValue();
        double uPrice = ((Number)this.unit_price.getValue()).doubleValue();
        int qty = ((Number)this.quantity.getValue()).intValue();
        String remks = this.remarks.getText().toString();
        String rawId=rawMaterialNamesMap.get(rname);
        double tot = qty*uPrice;
        if(logger.isDebugEnabled()){
        logger.debug(date+"***DATE***"+vname+"**Vendor Name***"+vid+"**Vendor ID***"+rname+"**Rawmat name"+rawId+"**rawmat ID**"+cheque+"**cheque**");
        logger.debug(amount+"**Amount**"+uPrice+"**uprice**"+qty+"**quantity**"+tot+"**total**");
        }
        pur.setPayment_mode(cheque);
        pur.setUnit_cost(uPrice);
        pur.setDate(date);
        pur.setAmt_paid(amount);
        pur.setQuantity(qty);
        pur.setVendor_id(vid);
        pur.setRaw_mat_id(rawId);
        pur.setRemarks(remks);
        pur.setTotal_amt(tot);
        if(pur!=null)
        purchase_list.add(pur);
        }
        catch(Exception e) {
            
            this.err_label.setText("Please Enter All the details");
        }
        finally {
            return;
        }
        
    }
    private void NextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextActionPerformed
        // TODO add your handling code here:
        
        this.saveAllData();
        this.resetActionPerformed(evt);
        if(logger.isDebugEnabled())
            logger.debug("Data successfully entered");
        this.err_label.setText("Data Successfully Added!!!");
    }//GEN-LAST:event_NextActionPerformed
    
    public boolean activateButtons() {
        if(this.amountpaid_active && this.payment_activ && this.quantity_activ
           && this.raw_acitv && this.unitprice_activ && this.vendor_activ)
           return true;
                   else
            return false;
    }
    
    public void proceed() {
        if (this.activateButtons()){
        this.Next.setEnabled(true);
        this.ok.setEnabled(true);
        this.cancel.setEnabled(false);
        }
    }
    private void quantityPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_quantityPropertyChange
        // TODO add your handling code here:
        this.Next.setEnabled(true);
        this.setTotalAmount();
    }//GEN-LAST:event_quantityPropertyChange

    public void setTotalAmount() {
        try {
       
        double uPrice = ((Number)this.unit_price.getValue()).doubleValue();
        int qty = ((Number)this.quantity.getValue()).intValue();
        tot_amount_to_pay = uPrice * qty;
        String tot_amt="Total Amount :"+tot_amount_to_pay;
        this.total_amount.setText(tot_amt);
        }
        catch(Exception e) {
         return;    
        }
    }
    private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
        // TODO add your handling code here:
        this.unit_price.setText("");
        this.amount_paid.setText("");
        this.quantity.setText("");
        this.remarks.setText("");
       // this.dateField
        this.err_label.setText("");
        this.bal_to_pay.setText("");
        this.total_amount.setText("");
        this.dateFiled.setDate(null);
        
    }//GEN-LAST:event_resetActionPerformed

    private void unit_pricePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_unit_pricePropertyChange
        // TODO add your handling code here:
       
        this.setTotalAmount();
    }//GEN-LAST:event_unit_pricePropertyChange

    private void amount_paidPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_amount_paidPropertyChange
        // TODO add your handling code here:
       
        this.setBalance();
        
    }//GEN-LAST:event_amount_paidPropertyChange

    private void dateFiledPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dateFiledPropertyChange
        // TODO add your handling code here:
        
        this.date=(Date)this.dateFiled.getDate();
        
    }//GEN-LAST:event_dateFiledPropertyChange
    
    public void setBalance() {
      
        try {
            if(this.total_amount.getText()=="" || tot_amount_to_pay==0 ){
                throw new Exception();
            }
          double paid=((Number)this.amount_paid.getValue()).doubleValue();  
          if(tot_amount_to_pay>paid) {
              double bal=tot_amount_to_pay-paid;
              String msg="Balance Amount :"+bal;
              this.bal_to_pay.setText(msg);
          }
        }
        catch(Exception e) {
            
            return;
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewPurchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewPurchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewPurchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewPurchase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewPurchase().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Next;
    private javax.swing.JLabel UnitPrice;
    private javax.swing.JFormattedTextField amount_paid;
    private javax.swing.JLabel bal_to_pay;
    private javax.swing.JButton cancel;
    private com.toedter.calendar.JDateChooser dateFiled;
    private javax.swing.JLabel err_label;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton ok;
    private javax.swing.JList payment_mode;
    private javax.swing.JFormattedTextField quantity;
    private javax.swing.JComboBox raw_material_combo;
    private javax.swing.JTextPane remarks;
    private javax.swing.JButton reset;
    private javax.swing.JLabel total_amount;
    private javax.swing.JFormattedTextField unit_price;
    private javax.swing.JComboBox vendor_combo;
    // End of variables declaration//GEN-END:variables

rb_vendor_persist vendor=rb_vendor_persist.getInstance();
List<rb_vendor> vendor_list_to_insert=new ArrayList<rb_vendor>();
List<rb_vendor> vendor_combo_to_insert = new ArrayList<rb_vendor>();
String[] vendor_names;
String[] raw_mat_names;
Map<String,String> vendorNamesMap =new HashMap<String, String>();
Map<String,String> rawMaterialNamesMap =new HashMap<String, String>();

boolean activate=false;
boolean vendor_activ=false;
boolean raw_acitv=false;
boolean payment_activ=false;
boolean quantity_activ=false;
boolean unitprice_activ=false;
boolean amountpaid_active=false;
CommonConstants.PAYMENT_MODE payment_mode_list;
private double tot_amount_to_pay;
List<rb_purchase> purchase_list = new ArrayList<rb_purchase>();
private Date date;
Logger logger = CommonsLogging.getInstance().getLogger();
}
